<template>
  <div class="pay-container">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付订单</XtxBreadItem>
      </XtxBread>

      <div v-if="order" class="pay-info">
        <div class="time">
          <span class="iconfont icon-queren2"></span>
          <div class="tip">
            <p>订单提交成功！请尽快完成支付</p>
            <p v-if="order.countdown > -1">支付还剩 <span>{{ countdownText }}</span>，超时后将取消订单</p>
            <p v-else>订单已超时</p>
          </div>
        </div>
        <p class="amount">应付总额: <span>¥{{ order.payMoney }}</span></p>
      </div>

      <p class="head">选择以下支付方式付款</p>

      <div class="pay-platform">
        <p class="head">支付平台</p>
        <div>
          <a class="btn wx" href="javascript:;"></a>
          <a :href="payUrl"
             class="btn alipay"
             target="_blank"
             @click="openDialog"></a>
        </div>
      </div>
      <div class="pay-way">
        <p class="head">支付方式</p>
        <div>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>

    <!-- 支付提示对话框 -->
    <XtxDialog v-model:visible="visibleDialog" title="正在支付...">
      <template v-slot:body>
        <div class="pay-wait">
          <img alt="" src="@/assets/images/load.gif">
          <div v-if="order">
            <p>如果支付成功: </p>
            <router-link :to="`/member/order/${order.id}`">查看订单详情></router-link>
            <p class="mar-top">如果支付失败: </p>
            <router-link to="/">查看相关疑问></router-link>
          </div>
        </div>
      </template>
    </XtxDialog>
  </div>
</template>

<script>
import API from '@/api/order'
import { useRoute, useRouter } from 'vue-router'
import { computed, inject, ref, watch } from 'vue'
import { usePayTime } from '@/hooks'
import { baseURL } from '@/utils/request'
import emitter from '@/utils/mitt'
import { useStore } from 'vuex'

export default {
  name: 'index',
  setup() {
    // 根据地址栏Id发请求获取订单数据
    const route = useRoute()
    const order = ref(null)
    // 支付提示对话框的显示状态控制
    const visibleDialog = ref(false)
    const store = useStore()

    const router = useRouter()

    // 支付地址   ==> 后台支付基准地址 + 支付页面地址 + 订单Id + 回调地址
    // 支付成功后回调地址 http://www.corho.com:8080/#/pay/callback?payResult=true&orderId=1475002978833272833
    const redirect = encodeURIComponent('http://www.corho.com:8080/#/pay/callback')
    const payUrl = computed(() => {
      return `${baseURL}pay/aliPay?orderId=${route.query.orderId}&redirect=${redirect}`
    })
    const {
      start,
      countdownText,
      time
    } = usePayTime()

    API.getOrderDetail(route.query.orderId)
        .then((res) => {
          order.value = res.result
          // 后端提供一个 countdown 倒计时秒数 res.result.countdown
          // 开启定时器
          if (res.result.countdown > -1) {
            start(res.result.countdown)
          }
        })
        .catch((err) => {

        })

    /**
     * 打开支付提示对话框逻辑, 现阶段只能做到1分钟后将这个对话框自动关闭
     * 无法实现定制化关闭时机, 为什么这里的监听器 或 事件中心不起作用呢
     */
    const openDialog = () => {
      visibleDialog.value = true

      setTimeout(() => {
        visibleDialog.value = false
        // 跳往该商品的订单详情页面
        router.push(`/member/order/${route.query.orderId}`)
        // 1分钟后自动跳转到订单详情页面
      }, 60000)
    }

    // const orderId = computed(() => {
    //   return store.state.orderId
    // })

    // const orderObj = inject('orderObj')
    //
    // // 根本监听不到变化了
    // watch(orderObj.orderId, (newVal) => {
    //   console.log('newVal', newVal)
    //   console.log('订单支付成功了, 准备关闭对话框, 跳往订单详情页面')
    //   // 关闭对话框
    //   visibleDialog.value = false
    //   // 跳往该商品的订单详情页面
    //   router.push(`/member/order/${route.query.orderId}`)
    // })

    // // todo 处理支付提示对话框的状态管理问题 跳往那个路径下的 --> 该商品的订单详情
    // 我觉得用事件中心, 或则使用 vuex, 不应该使用watch方式, 事件监听为什么没有监听到
    // emitter.on('close-dialog', (orderId) => {
    //   debugger
    //   console.log(orderId, 'from emitter')
    //   // 关闭对话框
    //   visibleDialog.value = false
    //   // 跳往该商品的订单详情页面
    //   router.push(`/member/order/${orderId}`)
    // })

    return {
      order,
      countdownText,
      time,
      payUrl,
      visibleDialog,
      openDialog,
    }
  }
}
</script>

<style lang="less" scoped>

.pay-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  margin-bottom: 20px;

  background-color: #fff;

  .time {
    display: flex;
    align-items: center;

    > span {
      color: #1dc779;
      font-size: 80px;
      margin-right: 10px;
    }

    .tip {
      p {
        font-size: 20px;

        &:last-child {
          margin-top: 5px;
          font-size: 16px;
          color: #999;

          span {
            vertical-align: middle;
          }
        }
      }
    }
  }

  .amount {
    color: #999;
    font-size: 16px;

    span {
      font-size: 20px;
      color: @priceColor;
    }
  }
}

.head {
  padding-left: 30px;
  height: 70px;
  line-height: 70px;

  border-bottom: 1px solid #f5f5f5;
  background-color: #fff;
  font-size: 16px;
}

.pay-platform {
  background-color: #fff;
  padding-bottom: 5px;

  .head {
    border-bottom: none;
  }
}

.btn {
  display: inline-block;
  width: 150px;
  height: 50px;
  margin-left: 30px;

  text-align: center;
  line-height: 48px;
  color: #666;
  border: 1px solid #e4e4e4;

  &:hover, &.active {
    border-color: @xtxColor;
  }

  &.wx {
    background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat 50%/contain;
  }

  &.alipay {
    background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat 50%/contain;
  }
}

.pay-way {
  background-color: #fff;
  padding-bottom: 70px;

  .head {
    border-bottom: none;
  }
}

.pay-wait {
  display: flex;
  justify-content: space-around;
  align-items: center;

  img {
    width: 180px;
    height: 180px;
  }

  > div {
    p {
      font-size: 14px;

      &.mar-top {
        margin-top: 30px;
      }
    }

    a {
      color: @xtxColor;
    }
  }
}
</style>
